﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Matrix = Microsoft.Xna.Framework.Matrix;

namespace Babylon.Animations
{
    public class CombinedMatrixAnimation : AnimationBase, IMatrixAnimation
    {
        public Matrix BaseValue { get; set; }
        private bool _currentDirty = true;
        private Matrix _current;
        public Matrix CurrentValue
        {
            get
            {
                if (_currentDirty)
                {
                    _current = Matrix.Identity;
                    for (int i = 0; i < _matrixAnimations.Count; i++)
                    {
                        _current *= _matrixAnimations[i].CurrentValue;
                    }
                    _currentDirty = false;
                }

                return _current;
            }
        }

        protected override void OnPaused()
        {
            base.OnPaused();
            for (int i = 0; i < _matrixAnimations.Count; i++)
                _matrixAnimations[i].Pause(CurrentTime);
            _currentDirty = true;
        }

        protected override void OnResumed()
        {
            base.OnResumed();
            for (int i = 0; i < _matrixAnimations.Count; i++)
                _matrixAnimations[i].Resume(CurrentTime);
            _currentDirty = true;
        }

        protected override void OnStarted()
        {
            base.OnStarted();
            for (int i = 0; i < _matrixAnimations.Count; i++)
                _matrixAnimations[i].Start(CurrentTime);
            _currentDirty = true;
        }

        protected override void OnStopped()
        {
            base.OnStopped();
            for (int i = 0; i < _matrixAnimations.Count; i++)
                _matrixAnimations[i].Stop(CurrentTime);
            _currentDirty = true;
        }
        protected override void OnUpdated()
        {
            base.OnUpdated();
            for (int i = 0; i < _matrixAnimations.Count; i++)
                _matrixAnimations[i].Update(CurrentTime);
            _currentDirty = true;
        }

        private readonly List<IMatrixAnimation> _matrixAnimations = new List<IMatrixAnimation>();
        public List<IMatrixAnimation> MatrixAnimations { get { return _matrixAnimations; } }
    }
}
